package com.dayatang.android.Activity;

import com.dayatang.android.Activity.Application.MyApplication;
import com.dayatang.android.config.Config;

import android.content.Intent;
import android.os.Bundle;
import android.preference.Preference;
import android.preference.PreferenceActivity;
import android.preference.Preference.OnPreferenceChangeListener;
import android.preference.Preference.OnPreferenceClickListener;

public class Settings extends PreferenceActivity implements
		OnPreferenceChangeListener, OnPreferenceClickListener {

	private Preference preference;
	
	private String serverAddresskey;
	
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		addPreferencesFromResource(R.xml.preferences);
		
		serverAddresskey = getResources().getString(R.string.settings_server_address_key);
		
		preference = findPreference(serverAddresskey);
		preference.setSummary(getServerAddress());
		
		preference.setOnPreferenceClickListener(this);
	}
	
	private String getServerAddress(){
		MyApplication application  = (MyApplication) this.getApplication();
		return application.getServerAddress();
	}
	
	public boolean onPreferenceClick(Preference arg0) {
		if(preference.getKey().equals(serverAddresskey)){
			Intent intent = new Intent(this,ServerInitActivity.class);
			intent.putExtra(Config.WHERE, Config.SETTINGS);
			startActivity(intent);
			finish();
			return true;
		}
		return false;
	}

	public boolean onPreferenceChange(Preference arg0, Object arg1) {
		// TODO Auto-generated method stub
		return false;
	}

}
